//
//  IHRefreshTableView.m
//  MyProject
//
//  Created by Pei on 16/6/22.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//
#import "IHRefreshTableView.h"

@implementation IHRefreshTableView
{
}

- (void) dealloc{
#if !__has_feature(objc_arc)
    [super dealloc];
#endif
}

- (void) removeFromSuperview{
    [_header removeFromSuperview];
    [_footer removeFromSuperview];
    [super removeFromSuperview];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _autoLoad = YES;
        _refreshType = RefreshTypeTop|RefreshTypeBottom;
        [self prepareMJRefresh];
    }
    return self;
}

- (id) initWithFrame:(CGRect)frame refreshType:(RefreshType)type{
    self = [super initWithFrame:frame];
    if(self){
        _autoLoad = YES;
        _refreshType = type;
        [self prepareMJRefresh];
    }
    return self;
}

- (id) initWithFrame:(CGRect)frame refreshType:(RefreshType)type andAutoLoad:(BOOL)autoLoad{
    self = [super initWithFrame:frame];
    if(self){
        _refreshType = type;
        _autoLoad = autoLoad;
        [self prepareMJRefresh];
    }
    return self;
}

-(id) initWithFrame:(CGRect)frame withStyle:(UITableViewStyle)style refreshType:(RefreshType)type andAutoLoad:(BOOL)autoLoad
{
    self = [super initWithFrame:frame style:style];
    if(self){
        _refreshType = type;
        _autoLoad = autoLoad;
        [self prepareMJRefresh];
    }
    return self;

}

- (void) prepareMJRefresh{
    if((self.refreshType & RefreshTypeTop) == RefreshTypeTop){
        [self addHeader];
        if([self isAutoLoad]){
            [_header beginRefreshing];
        }
    }
    if((self.refreshType & RefreshTypeBottom) == RefreshTypeBottom){
        [self addFooter];
    }
}

- (BOOL) isAutoLoad{
    return _autoLoad;
}

- (void) addHeader{
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self;
    __weak id weakSelf = self;
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [weakSelf beginHeaderRefresh:refreshView];
    };
    header.endStateChangeBlock = ^(MJRefreshBaseView *refreshView) {
        [weakSelf endHeaderRefresh:refreshView];
    };
    header.refreshStateChangeBlock = ^(MJRefreshBaseView *refreshView, MJRefreshState state) {
        switch (state) {
            case MJRefreshStateNormal:
                break;
                
            case MJRefreshStatePulling:
                break;
                
            case MJRefreshStateRefreshing:
                break;
            default:
                break;
        }
        [weakSelf refreshStateChange:state];
    };
    _header = header;
}

- (void) addFooter{
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self;
    __weak id weakSelf = self;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [weakSelf beginFooterRefresh:refreshView];
    };
    _footer = footer;
}

- (void) beginHeaderRefresh:(MJRefreshBaseView*) refreshView{
    if(self.refreshDelegate && [self.refreshDelegate respondsToSelector:@selector(beginRefreshHeader:refreshView:)]){
        [self.refreshDelegate beginRefreshHeader:self refreshView:refreshView];
    }else{
        [refreshView endRefreshing];
    }
}

- (void) beginFooterRefresh:(MJRefreshBaseView*) refreshView{
    if(self.refreshDelegate && [self.refreshDelegate respondsToSelector:@selector(beginRefreshFooter:refreshView:)]){
        [self.refreshDelegate beginRefreshFooter:self refreshView:refreshView];
    }else{
        [refreshView endRefreshing];
    }
}

- (void) refreshStateChange:(MJRefreshState) state{
    if(self.refreshDelegate && [self.refreshDelegate respondsToSelector:@selector(refreshStateDidChange:refreshState:)]){
        [self.refreshDelegate refreshStateDidChange:self refreshState:state];
    }
}

- (void)endHeaderRefresh:(MJRefreshBaseView *) refreshView {
    if (self.refreshDelegate && [self.refreshDelegate respondsToSelector:@selector(endRefreshHeader:refreshView:)]) {
        [self.refreshDelegate endRefreshHeader:self refreshView:refreshView];
    } else {
        [refreshView endRefreshing];
    }
}

- (void) refreshData{
    [_header beginRefreshing];
}

- (void) loadMore{
    [_footer beginRefreshing];
}


- (void)noMoreData
{
    if(self.tableFooterView == nil){
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 30)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 8, kDeviceWidth, 10)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"房源已全部加载";
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = kRGB(153, 153, 153);
        [v addSubview:label];
        self.tableFooterView = v;
    }else{
        self.tableFooterView.hidden = NO;
    }
    _footer.hidden = YES;
}

- (void)DQBListNoMoreData
{
    if(self.tableFooterView == nil){
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 30)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 8, kDeviceWidth, 12)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"已全部加载";
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = kRGBA(0, 0, 0, 0.54f);
        [v addSubview:label];
        self.tableFooterView = v;
    }else{
        self.tableFooterView.hidden = NO;
    }
    _footer.hidden = YES;
}

- (void)reloadFooterView
{
    _footer.hidden = NO;
    self.tableFooterView.hidden = YES;
    self.tableFooterView = nil;
}

@end
